<template>
  <!-- <div> -->
  <!-- 布局 使用element ui 第四个  将之前的div注释掉 -->
  <!-- <button type="info" @click="logout">退出</button> -->
  <!-- </div> -->
  <el-container class="home-container">
    <!-- 头部 -->
    <el-header>
      <div>
        <img src="../assets/mypic.jpg" alt />
        <span>电商后台管理系统</span>
      </div>
      <button type="info" @click="logout">退出</button>
    </el-header>
    <!-- 页面主题布局 -->
    <el-container>
      <!-- 侧边栏 -->
      <!-- element ui 自带的属性 控制侧边栏
      :collapse = 'toggleController'  控制侧边栏折叠
      :collapse-transition = 'false'显示折叠动画
      :router = 'true'  激活vue - router 模式  ,根据index的值跳转
      :default active 设置是否高亮 
       -->
      <el-aside width="toggleController ? '84px' : '200px' ">
        <div class="toggle-button" @click="toggleCollapse">侧边导航</div>
        <el-menu
          background-color="#333744"
          text-color="#fff"
          active-text-color="#ffd04b"
          unique-opened
          :collapse = 'toggleController'
          :collapse-transition = 'false'
          :router = 'true'
          :default-active = 'activePath'
        >
          <!-- 一级菜单  -->
          <!-- 默认是 index = '1'   后面也没有 :key =    
              这就造成点击一个菜单,所有菜单都会展开,原因是index重复 
              使用 :index动态绑定一个数值就可以解决 注意由于默认接受参数是字符串,而item.id是数字,所以报错
              在后面拼接一个空格就解决了   -->
          <el-submenu :index="item.id + ''" v-for="item in menuList" :key="item.id">
            <!-- 一级菜单模板区 -->
            <template slot="title">
              <!-- 图标 -->
              <i :class="iconList[item.id]"></i>
              <!-- 文本 -->
              <span>{{item.authName}}</span>
            </template>
              <!-- 二级菜单 
              路由模式 根据 index的值跳转
              此处希望是根据路径而不是id
              index="subItem.id改为path
              并且要手动修改路径,前面+ '/'
              显示高亮的标题栏
              -->
              <el-menu-item :index="'/' + subItem.path" 
              v-for="subItem in item.children" :key="subItem.id"
              @click="saveNavState('/' + subItem.path)"
              >
                <!-- 图标 -->
              <i class="el-icon-s-platform"></i>
              <!-- 文本 -->
              <span>{{subItem.authName}}</span>
              </el-menu-item>
          </el-submenu>
        </el-menu>
      </el-aside>
      <!-- 右边栏 -->
      <el-main>
        <!-- 路由占位符  -->
        <router-view></router-view>
      </el-main>
    </el-container>
  </el-container>
</template>


<script>
export default {
  data() {
    return {
      //左侧菜单数据
      menuList : [],
      //自定义图标库中,并且循环到每个一级菜单
      iconList : {
        '125':'iconfont icon-user',
        '103':'iconfont icon-tijikongjian',
        '101':'iconfont icon-shangpin',
        '102':'iconfont icon-danju',
        '145':'iconfont icon-baobiao',
      },
      toggleController: false,
      activePath: '',
    }

  },
  //定义生命周期函数,控制左侧菜单
  created (){
    this.getMenuList()
  //激活状态赋值
  this.activePath = window.sessionStorage.getItem('activePath')

  },
  methods: {
    //回到主页面,在退出登录时销毁token,并且返回到指定页面
    logout () {
      window.sessionStorage.clear()
      this.$router.push('/login')
      console.log(window.sessionStorage.getItem('token'))
    },
    async getMenuList () {
    const {data : res}  = await  this.$http.get('menus')
    if (res.meta.status !==200 ) return this.$message.err(res.meta.message)
     this.menuList = res.data
     console.log(res); 
    },
    //折叠按钮方法
    toggleCollapse () {
      this.toggleController=!this.toggleController
    },
    //保存连接激活状态 高亮设置 
    saveNavState (activePath) {
      window.sessionStorage.setItem('activePath',activePath)
      this.activePath =  activePath
    }

  }
}
</script>


<style lang="less" scoped>
.home-container {
  height: 100%;
}

.el-header {
  background-color: #373d41;
  display: flex;
  justify-content: space-between;
  padding-left: 0;
  padding-right: 0;
  align-items: center;
  color: #fff;
  font-size: 20px;
  > div {
    display: flex;
    align-items: center;
    span {
      margin-left: 15px;
    }
  }
}

.el-aside {
  background-color: #333744;
  .el-menu {
      border-right: 0;
  }
}

.el-main {
  background-color: #eaedf1;
}

.iconfont {
  margin: 10px;
}

.toggle-button {
  background-color: gray;
  font-size: 10px;
  line-height: 24px;
  color: #fff;
  text-align: center;
  letter-spacing: 10px;
  cursor: pointer;
}
</style>